# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from datetime import *
from scrapy.http import Request as sreq
from girl.items import city
class NewmoviecitySpider(CrawlSpider):
    name = 'newmoviecity'
    allowed_domains = ['cityheaven.net']
    start_urls = ['https://www.cityheaven.net/hokkaido/movie/1/']
    db_shell_path = "girl.dbShell.city.newgirl.main"
    tmp = {'girl.newdatapipelines.NewDataPipeline': 300}
    custom_settings = {
        'ITEM_PIPELINES':tmp
    }
    def parse3(self, response):
        sdata = self.crawler.stats.get_value('sdata',0)
        sdata = int(sdata) + int(response.css('.text-num.fs15 .red::text').extract_first(default=0))
        self.crawler.stats.set_value('sdata',sdata)
        return self.parse1(response)

    def parse1(self, response):
        f = response.css("li.moviebox")
        for x in f:
            url = x.css('a::attr("href")').extract_first()
            title = x.css('figcaption a::text').extract_first()
            cover = x.css('img::attr("data-echo")').extract_first()
            cover = cover or x.css('img::attr("src")').extract_first()
            # girlUrl = x.css(".movieName a::attr('href')").extract_first()
            tmp = {'thumb':cover,'title':title}
            yield sreq(response.urljoin(url),meta=tmp,callback=self.parse2)
        next = response.xpath("//*[@class='next']/@href").extract_first()
        if next:
            yield sreq(response.urljoin(next),callback=self.parse1)

    def parse2(self,response):
        item = city.movieItem()
        movie = response.css("#videofile::attr(src)").extract_first()
        item["thumb"] = response.meta.get("thumb")
        item["title"] = response.meta.get("title")
        item["girlUrl"] = response.css("#dynamic_image a::attr('href')").extract_first()
        item["url"] = response.url.replace("https://www.cityheaven.net", "")
        item["movie"] = movie
        item["date"] = str(date.today())
        yield item

    def parse(self, response):
        areas = response.css('.popbox li a::attr("href")').extract()
        del areas[0]
        del areas[1]
        urls = ['https://www.cityheaven.net%smovie/1/' % x for x in areas]
        for x in [sreq(x,callback=self.parse3) for x in urls]:
            yield x